//
//  SharedViewController.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/3.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "SharedViewController.h"


@interface SharedViewController ()

@property (nonatomic, copy) SharedViewControllerBlock sharedComplent;
/**  */
@property (nonatomic, strong) UIView *buttonView;
/** <#注释说明#> */
@property (nonatomic, strong) NSArray *buttonArray;
/** <#注释说明#> */
@property (nonatomic, strong) UIButton *clearBtn;
@end

@implementation SharedViewController

+ (instancetype)sharedViewController:(SharedViewControllerBlock)complent{
    SharedViewController *sharedVC = [[SharedViewController alloc]init];
    sharedVC.sharedComplent = complent;
    sharedVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    return sharedVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
    
    [self setUI];
}

- (void)setUI{
    _buttonArray = @[@"微信",@"朋友圈",@"微博",@"QQ",@"QQ空间",@"二维码",@"复制链接"];
    NSArray *imageArray = @[@"weixin",@"pengyouquan",@"weibo",@"QQ",@"QQkongjian",@"saoyisao",@"fuzhilianjie"];
    
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(10,ScreenHEIGH-(ScreenHEIGH/2) , ScreenWIDTH-20, (ScreenHEIGH/2)-90)];
    self.buttonView.backgroundColor = [UIColor whiteColor];
    self.buttonView.layer.cornerRadius = 5;
    self.buttonView.layer.masksToBounds = YES;
    [self.view addSubview:self.buttonView];
    
    CGFloat width = (ScreenWIDTH-90)/4;
    CGFloat height = width;
    for (int i = 0; i < _buttonArray.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10+(width+10)*(i%4), 20+(height+20)*(i/4), width, height)];
        button.tag = i;
        [button setTitle:_buttonArray[i] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonViewEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(button.frame.size.height+25, 0, 0, 0)];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.buttonView addSubview:button];
    }
    
    self.clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.clearBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.clearBtn.backgroundColor = [UIColor whiteColor];
    [self.clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.clearBtn.frame = CGRectMake(10, ScreenHEIGH-70, ScreenWIDTH-20, 50);
    [self.clearBtn addTarget:self action:@selector(clearBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    self.clearBtn.layer.cornerRadius = 5;
    self.clearBtn.layer.masksToBounds = YES;
    [self.view addSubview:self.clearBtn];
}

- (void)clearBtnEvent{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)buttonViewEvent:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:nil];
    if (_sharedComplent) {
        _sharedComplent(_buttonArray[button.tag]);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
